<?php

/**
 * This is the view file.
 * It contacts the control file to manipulate data
 *
 */
echo "<html><body>";
include '../db-control/inc/functions.php';
include '../model/Opportunity.php';
include '../db-control/dbOpportunity.php';

//update an internship if the user is trying to do so
if ($_POST['update'] != '') {
    $oppid = $_POST['update'];
    $o = new Opportunity(); //creates a new instance of Opportunity()
    $o->setOppID($oppid); //sets the Opportunity ID.
    $db = new dbOpportunity();
    $exists = $db->checkID($o); //checks to see if the ID is a real Opportunity
    if (!$exists) {
        echo "<b>Opportunity with ID '$oppid' does not exist...</b>";
    } else {
        $o->setOrgID($_POST['orgID']); //sets the Organization name.
        $o->setContactID($_POST['contactID']); //sets the Contact ID.
        $result = $db->updateOpportunity($o); //finally, updates the Opportunity with the data we've set
        if (!$result) {
            echo "<b> Could not update the opportunity with ID '$oppid'...</b>";
        }
    }
} else {

    if (isset($_POST['add'])) {
        $o = new Opportunity(); //creates a new Opportunity object (i.e. creates a new Oppertnity)
        $o->setOrgID($_POST['orgID']); //sets the opportunities Organization
        $o->setContactID($_POST['contactID']);  //sets the opportunities Conatact ID
        $db = new dbOpportunity();
        $result = $db->addOpportunity($o); //adds the Opportunity to the database with the information you've set
        if (!$result) {
            echo "<b>Could not add an Opportunity to the database.</b>";
        }
    }

    if (isset($_POST['delete_clicked'])) {
        $o = new Opportunity(); //creates a new Opportunity object (i.e. creates a new Opportunity)
        $o->setOppID($_POST['deleteOpportunity']);
        $db = new dbOpportunity(); 
        $result = $db->deleteOpportunity($o); //deletes the Opportunity
        if (!$result) {
            echo "<b>Could not delete opportunity with ID '$_POST[deleteOpportunity]'...</b>";
        }
    }
}


//displays the web form used for adding a new Opportunity
echo <<<_FORM


<form action='viewOpportunity.php' method='post'>
<table>
    <tr><td colspan='2'>Specify an ID to change a Opportunities data</td><td><input type='text' size='4' name='update' /></td></tr>
    <tr><td>Organization ID:</td><td><input type='text' value='' name='orgID' /></td></tr>
           <tr><td>Contact ID:</td><td><input type='text' value='' name='contactID' /></td></tr>
    <tr><td></td><td align='right'><input type='submit' value='Add Opportunity' name='add'/></td></tr>
</table>
</form>


_FORM;

displayOpportunities(); //displays the opportunity list


echo "</body></html>";

function displayOpportunities() {
    $db = new dbOpportunity(); //gets all the opportunities from the database
    $result = $db->getAllOpportunities();
    echo "<table width='200px'>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "<tr><td>$row[opportunityID]</td><td></td></tr>";
        echo "<tr><td align='right'>Organization ID:</td><td align='left'>$row[organizationID]</td></tr>";
        echo "<tr><td align='right'>Contact ID:</td><td align='left'>$row[contactID]</td></tr>";
        echo "<tr><td colspan='2' align='right'><form action='viewOpportunity.php' method='post'><input type='hidden' value='$row[opportunityID]' name='deleteOpportunity' /><input type='submit' value='Delete' name='delete_clicked' /></form></td><td></td></tr>";
        echo "<tr><td colspan='2'><hr /></td><td></td></tr>";
    }
    echo "</table>";
}
?>
